var kaformDesignerIdJS = [];
var addAndEditViewIndex = {
    init: function () {
        layui.use(kaValue.layuiUse, function () {
            var $ = layui.jquery;
            var formDesigner = layui.formDesigner;
            var render;
            var formDesignerContent = "";
            //获取Url 地址参数
            var formDesignerId = $.getUrlParam('formDesignerId');
            var buttonidsStrId = $.getUrlParam('buttonidsStrId');
            var d = top.ka_grid_rowsMap.get(buttonidsStrId);
            if (d == undefined || d == null || d == "") {
                d = {
                    data: [],
                    geturl: "",
                    submiturl: "",
                    submitway: ""
                };
            }
            var formdesignerFormurlId = "";
            var formdesignerFormurlId = $.getUrlParam('formdesignerFormurlId');
            if (formdesignerFormurlId != undefined && formdesignerFormurlId != null && formdesignerFormurlId != "") {
                var option = kaValue.getUrl("SysFormdesignerFormurGetInfo");
                    option.url = option.url + "/" + formdesignerFormurlId;
                    option.async = false;
                    option.success = function (re) {
                        if (re.code == 200) {
                            var buttonidsStrKaDataType = re.data.dataType;
                            var buttonidsStrKaData = re.data.data;
                            if (buttonidsStrKaData != undefined && buttonidsStrKaData != null && buttonidsStrKaData != "") {
                                if(buttonidsStrKaDataType == 2){
                                    d.data = JSON.parse(buttonidsStrKaData);
                                } else if(buttonidsStrKaDataType == 2){
                                    var option = kaValue.getUrl(buttonidsStrKaData);
                                    option.async = false;
                                    option.success = function (re) {
                                        d.data = re.data.data;
                                    }
                                    ka.ajax(option);
                                }
                            }
                            var KaGeturl = re.data.geturl;
                            if (KaGeturl != undefined && KaGeturl != null && KaGeturl != "") {
                                d.geturl = KaGeturl;
                            }
                            var KaSubmiturl = re.data.submiturl;
                            if (KaSubmiturl != undefined && KaSubmiturl != null && KaSubmiturl != "") {
                                d.submiturl = KaSubmiturl;
                            }
                            var KaSubmitway = re.data.submitway;
                            if (KaSubmitway != undefined && KaSubmitway != null && KaSubmitway != "") {
                                d.submitway = KaSubmitway;
                            }
                        }
                    }
                    option.error = function (re) {
                        return false;
                    }
                    ka.ajax(option);
            }
            if (Array.isArray(d.data)) {
                var idArray = [];
                for(var i in d.data){
                    idArray.push(d.data[i].id);
                }
                if(idArray.length > 0){
                    d.data = d.data[0];
                    d.data.ids = idArray.join(",");
                }
            }
            //获取内容
            formDesignerContent = addAndEditViewIndex.getFormDesignerContent(formDesignerId);
            //判断内容
            if (formDesignerContent != undefined && formDesignerContent != null && formDesignerContent != "") {
                var formData = {};
                if (d.geturl != undefined && d.geturl != null && d.geturl != "") {
                    formData = addAndEditViewIndex.getFormData(d.geturl, d);
                } else if(d.data.ids != undefined && d.data.ids != null && d.data.ids != ""){
                    formData.ids = d.data.ids;
                }
                var fvStr = $.getUrlParam("fvStr");
                if(fvStr != undefined && fvStr != null && fvStr != ""){
                    var fvStrObject = JSON.parse(decodeURI(decodeURIComponent(fvStr)));
                    for(var k in fvStrObject){
                        formData[fvStrObject[k].field] = fvStrObject[k].value;
                    }
                } 
                kaformDesignerIdJS = JSON.parse(formDesignerContent);
                var formDesignerOption = {
                    elem: '#addAndEditViewDiv',
                    data: kaformDesignerIdJS,
                    formDefaultButton: false,
                    viewOrDesign: true
                };
                if (formData != undefined && formData != null) {
                    formDesignerOption.formData = formData;
                }
                render = formDesigner.render(formDesignerOption);
            }
        });
    },
    getFormData: function (geturl, d) {
        var formData;
        var option = kaValue.getUrl(geturl);
        option.url = option.url + "/" + d.data.id;
        option.async = false;
        option.success = function (re) {
            if (re.code == 200) {
                formData = re.data;
            }
        }
        option.error = function (re) {
            return false;
        }
        ka.ajax(option);
        if(d.data.ids != undefined && d.data.ids != null && d.data.ids != ""){
            formData.ids = d.data.ids;
        }
        return formData;
    },
    getFormDesignerContent: function (formDesignerId) {
        var formDesignerContent = "";
        var option = kaValue.getUrl("formDesignerGetId");
        option.url = option.url + "/" + formDesignerId;
        option.async = false;
        option.success = function (re) {
            if (re.code == 200) {
                formDesignerContent = re.data.content;
            }
        }
        option.error = function (re) {
            return false;
        }
        ka.ajax(option);
        return formDesignerContent;
    },
    getData: function () {
        var formDesignerColumn = [];
        var formPreviewForm = {};
        var formDesignerId = $.getUrlParam('formDesignerId');
        var option = kaValue.getUrl("formdesignerColumnAllList");
        option.async = false;
        option.data = {
            "fid": formDesignerId
        }
        option.success = function (re) {
            if (re.code == 200) {
                formDesignerColumn = re.data;
            }
        }
        option.error = function (re) {
            return false;
        }
        ka.ajax(option);
        for (var i in formDesignerColumn) {
            if (formDesignerColumn[i].tag == "singleSelect") {
                formPreviewForm[formDesignerColumn[i].name] = "";
                var xmKaSelect = top.ka_grid_formDesignerSelectMap.get(formDesignerColumn[i].tag + "" + formDesignerColumn[i].name);
                var selectArr = xmKaSelect.getValue();
                var selectDataValues = [];
                for (var j in selectArr) {
                    selectDataValues.push(selectArr[j].value);
                }
                formPreviewForm[formDesignerColumn[i].name] = selectDataValues.join(",");
            } else if (formDesignerColumn[i].tag == "input" || formDesignerColumn[i].tag == "hiddenInput" || formDesignerColumn[i].tag == "chooseToBringBack" || formDesignerColumn[i].tag == "textarea" || formDesignerColumn[i].tag == "date") {
                var data = layui.form.val('formPreviewForm');
                formPreviewForm[formDesignerColumn[i].name] = data[formDesignerColumn[i].name];
            }
            if (formDesignerColumn[i].required == "true") {
                if (formPreviewForm[formDesignerColumn[i].name] == undefined || formPreviewForm[formDesignerColumn[i].name] == null || formPreviewForm[formDesignerColumn[i].name] == "") {
                    kaMsg.error({ "code": "500", "msg": (formDesignerColumn[i].label + "不能为空！") });
                    return null;
                }
            }
        }
        return formPreviewForm;
    }
}